/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						read_script_target_from_map.h

	$Header: /heroes4/read_script_target_from_map.h $

	$NoKeywords: $

 ************************************************************************/

#if !defined( READ_SCRIPT_TARGET_FROM_MAP_H_INCLUDED )
#define READ_SCRIPT_TARGET_FROM_MAP_H_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "script_target_type.h"
#include "sized_int_types.h"
#include "streambuf_operators.h"

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
template < typename t_target >
inline bool read_script_target_from_map(
	std::streambuf &	stream,
	t_target &			target )
{
	t_target result = t_target( get< t_uint8 >( stream ) );
	if ( result < 0 || result >= t_script_target_type_traits< t_target >::k_count )
		return false;
	target = result;
	return true;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
template < typename t_target >
struct t_read_script_target_from_map_helper
{
	static bool read(
		std::streambuf &	stream,
		int					format_version,
		t_target &			target )
	{
		return read_script_target_from_map( stream, target );
	}
};

template <>
struct t_read_script_target_from_map_helper< t_script_player_target >
{
	static bool read(
		std::streambuf &			stream,
		int							format_version,
		t_script_player_target &	target )
	{
		if ( format_version >= 2 )
			return read_script_target_from_map( stream, target );

		t_old_script_player_target result = t_old_script_player_target( get< t_uint8 >( stream ) );
		if ( result < 0 || result >= k_old_script_player_target_count )
			return false;
		target = translate( result );
		return true;
	}
};

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
template < typename t_target >
inline bool read_script_target_from_map(
	std::streambuf &	stream,
	int					format_version,
	t_target &			target )
{
	return	t_read_script_target_from_map_helper< t_target >::read(
				stream,
				format_version,
				target );
}

#endif // !defined( READ_SCRIPT_TARGET_FROM_MAP_H_INCLUDED )
